<?php
namespace app\common\model;
class Actionlog extends \think\Model{	
    protected $insert = ['log_addtime','log_ip'];	
	protected function setLogAddtimeAttr($value){
		return time();
	}
    protected function setLogIpAttr(){
        return bindec(decbin(ip2long(request()->ip())));
    }
    //自定义日志标题
    protected static $title = '';
    //自定义日志内容
    protected static $content = '';	
	
    public static function setTitle($title){
        self::$title = $title;
    }

    public static function setContent($content){
        self::$content = $content;
    }
    public static function record($params = ''){
		$url=strtolower(request()->module()).'/'.strtolower(request()->controller()).'/'.strtolower(request()->action());
		$action=db('action')->where(['name'=>$url,'status'=>1])->find();
		if($action){
        $admin = session('member_auth');
        $admin_id = $admin ? $admin['member_id'] : 0 ;
        $content = self::$content;
        if (!$content){
            $content = request()->param();
            foreach ($content as $k => $v){
                if (is_string($v) && strlen($v) > 100 || stripos($k, 'password') !== false || stripos($k, 'pwd') !== false){
                    unset($content[$k]);
                }
            }
        }
		$recurrence=[];
		if(!empty($params)){
			$recurrence=$params->getData();	
		}
		$data=[];
		$data['log_uid']=(string)$admin_id;
		$data['log_ip']=bindec(decbin(ip2long(request()->ip())));
		$data['log_url']=(string)request()->url();
		$data['log_action']=$url;
		$data['log_title']=(string)$recurrence['msg'];
		$data['log_info']=!is_scalar($recurrence) ? json_encode($recurrence) : $recurrence;
		$data['log_data']=!is_scalar($content) ? json_encode($content) : $content;
		$data['log_useragent']=request()->server('HTTP_USER_AGENT');
		$data['log_status']=(string)$recurrence['code'];
		$data['log_addtime']=time();
		$action=db('action')->where(['name'=>$data['log_action'],'status'=>1])->find();
            self::create($data);
		}
    }

    public function admin(){
        return $this->belongsTo('member','member_id','admin_uid')->setEagerlyType(0);
    }

}
